class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

root=TreeNode(1)
root.left=TreeNode(2)
root.right=TreeNode(3)
root.left.right=TreeNode(5)
root.right.right=TreeNode(4)

def rightSideView(root):
    if root==None:
        return []
    #做一个先进先出的队列
    queue=[root]
    ans=[root.val]
    while len(queue)>0:
        queueLen=len(queue)
        while queueLen>0:
            node=queue.pop(0)
            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)
            queueLen-=1
        if len(queue)>0:
            ans.append(queue[len(queue)-1].val)
    print(ans)
rightSideView(root)
